home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
160_01
/
sources
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-24
|
35KB
|
1,745 lines
###asst.c
/* EXAMPLES OF ASSIGNMENT
* (No output is produced)
*/
main()
{
char c;
short i;
c = 'A';
i = 65;
c = 'X';
i = -4;
}
###asst2.c
/* asst2 - print assigned values
*/
main()
{
char c;
short i;
c = 'A';
i = 65;
printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
c, c, c, c);
printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
i, i, i, i);
c = 'X';
i = -4;
printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
c, c, c, c);
printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
i, i, i, i);
}
###badpow.c
/* badpow - demonstrate power function (argument mismatch error)
*/
#include "local.h"
#include <math.h>
main()
{
short i;
for (i = 0; i < 10; ++i)
printf("2 to the power %d equals %.0f\n",
i, pow(2, i));
exit(SUCCEED);
}
###bdrill.c
/* bdrill - binary arithmetic practice
*/
#include "local.h"
#include <ctype.h>
main()
{
short a, b;
short getbin();
void putbin();
while ((a = getbin()) != 0 && (b = getbin()) != 0)
{
printf("\n a = ");
putbin(a);
printf("\n b = ");
putbin(b);
printf("\na + b = ");
putbin(a + b);
printf("\na & b = ");
putbin(a & b);
printf("\na | b = ");
putbin(a | b);
printf("\na ^ b = ");
putbin(a ^ b);
printf("\n");
}
}
<:IT:>(<:fC:>getbin<:IT:> function goes here)<:fC:>
<:IT:>(<:fC:>putbin<:IT:> function goes here)<:fC:>
###bits.c
/* bits - examples of bitwise operations
*/
#include "local.h"
main()
{
bits b1, b2;
b1 = 0xF0F0 & 0x1234;
b2 = b1 | 0x60;
printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
b1 = ~1 & 0307;
b2 = (bits)b1 >> 2;
printf("b1=0%03o, b2=0%03o\n", b1, b2);
b1 = 0xF001 | 0x8801;
b2 = b1 & 0xB800;
printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
}
###blast.c
/* blast - print countdown
*/
main()
{
short n;
for (n = 10; n >= 0; n = n - 1)
printf("%d\n", n);
printf("Blast off!\n");
}
###blast2.c
/* blast2 - print countdown
*/
main()
{
short n;
for (n = 10; n >= 0; n = n - 1)
{
printf("%d\n", n);
if (n == 3)
printf("We have ignition!\n");
}
printf("Blast off!\n");
}
###cadd.c
/* cadd - add two COMPLEX numbers
*/
#include "complex.h"
COMPLEX cadd(x, y)
COMPLEX x, y;
{
COMPLEX z;
z.real = x.real + y.real;
z.imag = x.imag + y.imag;
return (z);
}
###cap.c
/* cap - capitalize initial letters
*/
#include "local.h"
#include <ctype.h>
main()
{
metachar c;
while (isspace(c = getchar()))
putchar(c);
while (c != EOF)
{
putchar(toupper(c));
while (!isspace(c = getchar()))
putchar(c);
while (isspace(c))
{
putchar(c);
c = getchar();
}
}
}
/* toupper - convert lower-case letter to lower case
*/
metachar toupper(c)
metachar c;
{
return (islower(c) ? c + 'A' - 'a' : c);
}
###cap2.c
/* cap - capitalize initial letters
*/
#include "local.h"
#include <ctype.h>
main()
{
tbool waswhite; /* was preceding character a whitespace? */
metachar c;
for (waswhite = YES; (c = getchar()) != EOF; waswhite = isspace(c))
{
if (!isspace(c) && waswhite)
putchar(toupper(c));
else
putchar(c);
}
}
/* toupper - convert lower-case letter to lower case
*/
metachar toupper(c)
metachar c;
{
return (islower(c) ? c + 'A' - 'a' : c);
}
###codes1.c
/* codes1 - print ASCII codes
*/
main()
{
short c;
for (c = 0; c <= 127; c = c + 1)
{
printf("%3d 0x%02x 0%03o", c, c, c);
if (' ' <= c && c <= '~')
printf(" '%c'", c);
if ('0' <= c && c <= '9')
printf(" digit");
if ('A' <= c && c <= 'Z')
printf(" uppercase");
if ('a' <= c && c <= 'z')
printf(" lowercase");
printf("\n");
}
}
###codes2.c
/* codes2 - print ASCII codes
*/
#include "local.h"
main()
{
short c;
while ((c = getchar()) != EOF)
{
printf("%3d 0x%02x 0%03o", c, c, c);
if (' ' <= c && c <= '~')
printf(" '%c'", c);
if ('0' <= c && c <= '9')
printf(" digit");
if ('A' <= c && c <= 'Z')
printf(" uppercase");
if ('a' <= c && c <= 'z')
printf(" lowercase");
printf("\n");
}
}
###codes3.c
/* codes3 - print ASCII codes
*/
#include "local.h"
main()
{
metachar c; /* return from getchar: char or EOF */
while ((c = getchar()) != EOF)
{
printf("%3d 0x%02x 0%03o", c, c, c);
if (' ' <= c && c <= '~')
printf(" '%c'", c);
if ('0' <= c && c <= '9')
printf(" digit");
if ('A' <= c && c <= 'Z')
printf(" uppercase");
if ('a' <= c && c <= 'z')
printf(" lowercase");
printf("\n");
}
}
###codes4.c
/* codes1 - print ASCII codes
*/
#include "local.h"
#include <ctype.h>
main()
{
metachar c;
for (c = 0; c <= 127; c = c + 1)
{
printf("%3d 0x%02x 0%03o", c, c, c);
if (isprint(c))
printf(" '%c'", c);
if (isdigit(c))
printf(" D");
if (isupper(c))
printf(" UC");
if (islower(c))
printf(" LC");
if (isalpha(c))
printf(" L");
if (isalnum(c))
printf(" AN");
if (isspace(c))
printf(" S");
if (ispunct(c))
printf(" P");
if (iscntrl(c))
printf(" C");
printf("\n");
}
}
###collect.c
#include <std.h>
#define MAXL 512
/* collect line numbers on common index entries
*/
main(ac, av)
COUNT ac;
TEXT **av;
{
TEXT c, *s;
TEXT this[MAXL], next[MAXL];
c = (ac <= 1) ? '\t' : av[1][0];
s = (ac <= 2) ? "\n\t" : av[2];
if (getfmt("%512p", this) <= 0)
this[0] = '\0';
putfmt("%p", this);
while (this[0])
{
if (getfmt("%512p", next) <= 0)
next[0] = '\0';
if (mthru(this, next, c))
{
putfmt("%p", s);
putfmt("%p", suf(next, c));
}
else
{
putfmt("\n");
cpystr(this, next, NULL);
putfmt("%p", this);
}
}
}
/* match strings thru null or c
*/
mthru(pa, pb, c)
TEXT *pa, *pb, c;
{
for ( ; *pa == *pb && *pa != c; pa++)
if (*pb == '\0')
return(1);
else
pb++;
return (*pa == *pb || *pa == '\0' && *pb == c);
}
/* locate suffix of s following c
*/
suf(s, c)
TEXT *s, c;
{
while (*s && *s++ != c)
;
return (s);
}
###copy.c
/* copy - copy input to output
*/
#include "local.h"
main()
{
metachar c;
while ((c = getchar()) != EOF)
putchar(c);
exit(SUCCEED);
}
###copy2.c
/* copy2 - copy input to output
*/
#include "local.h"
main()
{
char s[BUFSIZ];
while (getln(s, BUFSIZ) != EOF)
printf("%s", s);
}
###copy3.c
/* copy3 - most efficient file copy
*/
#include "local.h"
main()
{
char s[BUFSIZ]; /* array for characters */
short i; /* number of characters read */
while (0 != (i = read(STDIN, s, BUFSIZ)))
{
if (i < 0)
error("I/O error on read\n", "");
else if (i != write(STDOUT, s, i))
error("I/O error on write\n", "");
}
exit(SUCCEED);
}
/* error - print fatal error message
*/
void error(s1, s2)
char s1[], s2[];
{
write(STDERR, s1, strlen(s1));
write(STDERR, " ", 1);
write(STDERR, s2, strlen(s2));
write(STDERR, "\n", 1);
exit(FAIL);
}
###dmpdem.c
/* dump - print memory bytes
* (Warning - some usages may be non-portable)
*/
#include "local.h"
#define LINESIZE 16
#define BYTEMASK 0xFF
void dump(s, n)
char s[]; /* byte address to be dumped */
unsigned n; /* number of bytes to dump */
{
unsigned i; /* byte counter */
for (i = 0; i < n; ++i)
{
if (i % LINESIZE == 0)
printf("\n%08x: ", &s[i]);
printf(" %02x", s[i] & BYTEMASK);
}
printf("\n");
}
/* dmpdem - demonstrate dump function
*/
main()
{
char msg[16];
double d = 100.;
strncpy(msg, "testing 1 2 3\n", sizeof(msg));
/* case 1 - quite proper */
dump(msg, sizeof(msg));
/* case 2 - ok, but output will vary with machine */
dump(&d, sizeof(d));
/* case 3 - non-portable, may cause hardware error */
dump(0x40, 4);
exit(SUCCEED);
}
###dump.c
/* dump - print memory bytes
* (Warning - some usages may be non-portable)
*/
#include "local.h"
#define LINESIZE 16
void dump(s, n)
char s[]; /* byte address to be dumped */
unsigned n; /* number of bytes to dump */
{
unsigned i; /* byte counter */
for (i =